package com.xzzz.irda.tracker.core;

import cn.hutool.core.util.StrUtil;
import com.xzzz.common.base.util.json.JsonUtil;
import com.xzzz.irda.tracker.core.common.TrackerConstants;

import java.util.TreeMap;

/**
 * Trace 对 HTTP 请求处理的相关工具类
 */
public class TrackerUtil {

    /**
     * 构造一个 Trace 请求头
     */
    public static String buildHeader(String type) {
        String traceId = Tracker.getTraceId();
        String spanParentId = Tracker.getSpanId();
        return buildHeader(type, traceId, spanParentId);
    }

    /**
     * 构造一个 Trace 请求头
     */
    public static String buildHeader(String type, String traceId, String spanParentId) {
        if (StrUtil.isNotBlank(traceId) && StrUtil.isNotBlank(spanParentId)) {
            return String.format("%s|%s|%s", traceId, spanParentId, type);
        }
        return "";
    }

    public static boolean checkIgnoreApi(String spanName) {
        for (String api : TrackerConstants.ignoreInnerApi) {
            if (spanName.contains(api)) {
                return true;
            }
        }
        return false;
    }

    public static boolean checkIgnoreSql(String spanName) {
        for (String api : TrackerConstants.ignoreInnerSql) {
            if (spanName.contains(api)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        String s = "{}";
        TreeMap<String, String> records = new TreeMap<>();

        System.out.println(JsonUtil.toJson(records));
        System.out.println(JsonUtil.toMap(s));
    }
}
